//
//  DBManager.m
//  insyte
//
//  Created by Vu Dang Tuan on 6/22/15.
//  Copyright (c) 2015 Robert. All rights reserved.
//

#import "DBManager.h"
static DBManager *sharedInstance = nil;
static sqlite3 *database = nil;
static sqlite3_stmt *statement = nil;

@implementation DBManager
+(DBManager*)getSharedInstance{
    if (!sharedInstance) {
        sharedInstance = [[super allocWithZone:NULL]init];
        [sharedInstance createDB];
    }
    return sharedInstance;
}
-(BOOL)createDB{
    NSString *docsDir;
    NSArray *dirPaths;
    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = dirPaths[0];
    // Build the path to the database file
    databasePath = [[NSString alloc] initWithString:
                    [docsDir stringByAppendingPathComponent: @"user.db"]];
    BOOL isSuccess = YES;
    NSFileManager *filemgr = [NSFileManager defaultManager];
    if ([filemgr fileExistsAtPath: databasePath ] == NO)
    {
        const char *dbpath = [databasePath UTF8String];
        if (sqlite3_open(dbpath, &database) == SQLITE_OK)
        {
            char *errMsg;
            const char *sql_stmt = "create table if not exists userDetail (id text primary key, first_name text, last_name text, email text, token text)";
            if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)
                != SQLITE_OK)
            {
                isSuccess = NO;
                NSLog(@"Failed to create table");
            }
            sqlite3_close(database);
            return  isSuccess;
        }
        else {
            isSuccess = NO;
            NSLog(@"Failed to open/create database");
        }
    }
    return isSuccess;
}
-(void)deleteUser
{
    NSString *query = [NSString stringWithFormat:@"DELETE from userDetail"];
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        const char *insert_stmt = [query UTF8String];
        sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
        }
        else {
        }
        sqlite3_reset(statement);
    }
}

- (BOOL)saveUser:(User *)user
{
    [self deleteUser];
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        NSString *insertSQL = [NSString stringWithFormat:@"insert into userDetail (id, first_name, last_name, email, token) values(\"%@\",\"%@\", \"%@\", \"%@\",\"%@\")",user.id, user.firstName, user.lastName,user.email, user.token];
        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            sqlite3_reset(statement);
            return YES;
        }
        else {
            sqlite3_reset(statement);
            return NO;
        }
    }
    return NO;
}
- (User *)getUser
{
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        NSString *querySQL = @"select * from userDetail";
        const char *query_stmt = [querySQL UTF8String];
        User *rs = [[User alloc] init];
        if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
            if (sqlite3_step(statement) == SQLITE_ROW)
            {
                rs.id = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 0)];
                rs.firstName = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 1)];
                rs.lastName = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 2)];
                rs.email = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 3)];
                rs.token = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 4)];
                sqlite3_reset(statement);
                return rs;
            }
            else{
                sqlite3_reset(statement);
                NSLog(@"Not found");
                return nil;
            }
        }
    }
    return nil;
}

@end
